package org.rsbot.script.randoms;

import org.rsbot.script.Random;
import org.rsbot.script.ScriptManifest;
import org.rsbot.script.wrappers.RSInterface;
import org.rsbot.script.wrappers.RSInterfaceChild;
import org.rsbot.script.wrappers.RSNPC;
import org.rsbot.script.wrappers.RSObject;

/**
 * @author Secret Spy
 * @version 2.4 - 02/11/11
 */
@ScriptManifest(authors = {"Secret Spy"}, name = "SandwichLady", version = 2.4)
public class SandwhichLady extends Random {

    final static int IDInterfaceSandwhichWindow = 297;
    final static int IDInterfaceSandwhichWindowText = 48;
    final static int IDInterfaceTalk = 243;
    final static int IDInterfaceTalkText = 7;
    final static int[] IDItems = {10728, 10732, 10727, 10730, 10726, 45666, 10731};
    final static int IDSandwhichLady = 8630;
    final static String[] NameItems = {"chocolate", "triangle", "roll", "pie", "baguette", "doughnut", "square"};
    final boolean Debug = false;

    @Override
    public boolean activateCondition() {
        final RSNPC Lady = getNearestNPCByID(IDSandwhichLady);
        return Lady != null;
    }

    @Override
    public int loop() {
        final RSInterface Chat1 = getInterface(243);
        if (Chat1.isValid()) {
            atInterface(243, 7);
            return random(900, 1200);
        }
        if (!activateCondition()) {
            return -1;
        }
        //Leaves random
        int[] PortalID = {12731, 11373};
        if (getInterface(242).getComponent(4).containsText("The exit portal's")) {
            RSObject Portal = getNearestObjectByID(PortalID);
            if (Portal != null) {
                if (!tileOnScreen(Portal.getLocation())) {
                    walkTileOnScreen(Portal.getLocation());
                } else {
                    atObject(Portal, "Enter");
                    return random(1000, 1500);
                }
            }
        }
        // Check if we need to press continue, on the talk interface
        if (getInterface(SandwhichLady.IDInterfaceTalk).isValid()) {
            atInterface(SandwhichLady.IDInterfaceTalk, SandwhichLady.IDInterfaceTalkText);
            return random(900, 1200);
        }

        // Check if the sandwhich window is open
        if (getInterface(SandwhichLady.IDInterfaceSandwhichWindow).isValid()) {
            final RSInterface Window = getInterface(SandwhichLady.IDInterfaceSandwhichWindow);
            int offset = -1;
            final String txt = Window.getComponent(SandwhichLady.IDInterfaceSandwhichWindowText).getText();
            for (int off = 0; off < SandwhichLady.NameItems.length; off++) {
                if (txt.contains(SandwhichLady.NameItems[off])) {
                    offset = off;
                    if (Debug) {
                        log.info("Found: " + SandwhichLady.NameItems[off] + " - ID: " + SandwhichLady.IDItems[off]);
                    }
                }
            }
            for (int i = 7; i < 48; i++) {
                final RSInterfaceChild Inf = Window.getComponent(i);

                if (Debug) {
                    log.info("child[" + i + "] ID: " + Inf.getModelID() + " == " + SandwhichLady.IDItems[offset]);
                }
                if (Inf.getModelID() == SandwhichLady.IDItems[offset]) {
                    atInterface(Inf);
                    sleep(random(900, 1200)); // Yea, use a sleep here! (Waits are allowed in randoms.)
                    if (!getInterface(SandwhichLady.IDInterfaceSandwhichWindow).isValid()) {
                        log.info("Solved the Sandwich Lady, by eating a " + SandwhichLady.NameItems[offset]);
                        sleep(6000);
                        return random(900, 1500);
                    }
                }

            }
            return random(900, 1200);
        }
        final RSInterface Chat2 = getInterface(242);
        if (Chat2.isValid()) {
            atInterface(242, 6);
            return random(900, 1200);
        }
        // Talk to the lady
        final RSNPC Lady = getNearestNPCByID(SandwhichLady.IDSandwhichLady);
        if (Lady != null && Lady.getAnimation() == -1) {
            if (!tileOnScreen(Lady.getLocation())) {
                walkTileOnScreen(Lady.getLocation());
            } else {
                atNPC(Lady, "talk");
                return random(1000, 1500);
            }
        }
        return random(900, 1200);
    }
}
